/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ModificacionAlquilerViewImpl.java
 *
 * 
 */
package vista.alquiler;

import controlador.AlquilerController;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.DefaultComboBoxModel;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import modelo.AlquilerModel;
import modelo.PistaModel;
import modelo.PistaModelImpl;
import modelo.persistencia.hibernate.pojo.AlquilerImpl;
import modelo.persistencia.hibernate.pojo.Pista;
import modelo.persistencia.hibernate.pojo.PistaImpl;
import vista.AbstractViewImpl;
import vista.informe.InformeVisualImpl;
import vista.pista.PistaTableAndComboModel;

/**
 *
 * @author
 */
public class ModificacionAlquilerViewImpl extends AbstractViewImpl<AlquilerController, AlquilerModel> implements AlquilerView {

    /** Creates new form ModificacionAlquilerViewImpl */
    public ModificacionAlquilerViewImpl() {
        pistaComboModel = new PistaTableAndComboModel();
        this.setPistaModel(new PistaModelImpl());
        initComponents();
    }
    AlquilerImpl a = null;
    PistaImpl pistaSeleccionada=null;

    // para el combobox
    private PistaTableAndComboModel pistaComboModel;
    PistaModel pistaModel;

    public PistaModel getPistaModel() {
        return pistaModel;
    }

    public void setPistaModel(PistaModel pistaModel) {
        this.pistaModel = pistaModel;
    }
    ///--------------------

    public void setAlquiler(AlquilerImpl a) {
        this.a = a;


        DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        DateFormat sdh = new SimpleDateFormat("HH:mm");



        jDNI.setText(a.getCliente().getDni());
        jNombre.setText(a.getCliente().getNombre());
        jApellidos.setText(a.getCliente().getApellidos());
        jPista.setText(a.getPista().getNombre());
        jFecha.setText(sdf.format(a.getHora()));
        jHora.setText(sdh.format(a.getHora()));
        // jFecha.setText(sdf.format(fecha));
        establecerHorasDisponibles();


    }

    public void actualizarAlquileres(List<AlquilerImpl> alquiler) {


        JPanel borderlaoutpanel = new JPanel();
        this.jListaAlquileres.setViewportView(borderlaoutpanel);
        borderlaoutpanel.setLayout(new BorderLayout(0, 0));

        JPanel columnpanel = new JPanel();
        borderlaoutpanel.add(columnpanel, BorderLayout.NORTH);
        columnpanel.setLayout(new GridLayout(0, 1, 0, 1));
        columnpanel.setBackground(Color.gray);


        for (int i = 0; i < alquiler.size(); i++)//desde 0 hasta el final de la lista de alquileres
        {
            JPanel rowPanel = new JPanel();
            rowPanel.setPreferredSize(new Dimension(300, 43));
            columnpanel.add(rowPanel);
            rowPanel.setLayout(null);


            FilaAlquiler fila = new FilaAlquiler((AlquilerImpl) alquiler.get(i), this, "Modificar");

            fila.setBounds(0, 0, 900, 42);
            rowPanel.add(fila);


            /*
            if(i%2==0)
            rowPanel.setBackground(SystemColor.inactiveCaptionBorder);
             */
        }




        JPanel panel = new JPanel();
        panel.add(new JButton()); //esto es solo un ejemplo de agregar un boton al JPAnel

        //aqui se agrega el JPanel al JScrollPane
        JScrollPane scroll = new JScrollPane(panel);

        //ahora agregamos el JScrollPane a la ventana
        this.jListaAlquileres.add(scroll);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jHora = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jDNI = new javax.swing.JTextField();
        jFecha = new javax.swing.JTextField();
        jNombre = new javax.swing.JTextField();
        jApellidos = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPista = new javax.swing.JTextField();
        listaHoras = new javax.swing.JComboBox();
        listaPistasM = new javax.swing.JComboBox();
        jListaAlquileres = new javax.swing.JScrollPane();
        jButton2 = new javax.swing.JButton();

        jPanel2.setBackground(java.awt.Color.white);

        jLabel5.setFont(new java.awt.Font("Lucida Grande", 1, 24));
        jLabel5.setText("Modificar de alquiler");
        jLabel5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(324, 324, 324)
                .addComponent(jLabel5)
                .addContainerGap(333, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(jLabel5)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        jButton1.setText("Guardar Cambios");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel7.setText("Pista:");

        jLabel4.setText("Fecha:");

        jLabel3.setText("Apellidos:");

        jHora.setEditable(false);
        jHora.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jHoraActionPerformed(evt);
            }
        });

        jLabel6.setText("Hora:");

        jDNI.setEditable(false);
        jDNI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDNIActionPerformed(evt);
            }
        });

        jFecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jFechaActionPerformed(evt);
            }
        });

        jNombre.setEditable(false);
        jNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNombreActionPerformed(evt);
            }
        });

        jApellidos.setEditable(false);
        jApellidos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jApellidosActionPerformed(evt);
            }
        });

        jLabel2.setText("Nombre:");

        jLabel1.setText("DNI:");

        jPista.setEditable(false);
        jPista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPistaActionPerformed(evt);
            }
        });

        listaHoras.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ninguna" }));

        listaPistasM.setModel(pistaComboModel);
        listaPistasM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listaPistasMActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(146, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(jLabel1))
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jDNI, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jFecha, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(listaHoras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jHora, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel7)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jApellidos, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                            .addComponent(jPista, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(listaPistasM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(21, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6)
                    .addComponent(jHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(listaHoras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(listaPistasM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jButton2.setText("Listar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(415, 415, 415))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jListaAlquileres, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 915, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jListaAlquileres, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(52, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jApellidosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jApellidosActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jApellidosActionPerformed

    private void jFechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jFechaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jFechaActionPerformed

    private void jPistaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPistaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jPistaActionPerformed

    private void jNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNombreActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jNombreActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        //List l=getModel().listar();


        refresh();

        /*
        for(Object alquiler:l)
        {
        AlquilerImpl a=(AlquilerImpl)alquiler;
        FilaAlquiler f=new FilaAlquiler(a, this);
        jListaAlquileres.add(f);


        }
         * */

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jDNIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDNIActionPerformed
    }//GEN-LAST:event_jDNIActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        try{

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy HH:mm");

        Date hora = null;

        try {
            hora = sdf.parse(jFecha.getText() + " " + listaHoras.getSelectedItem());
        } catch (ParseException ex) {
            Logger.getLogger(ModificacionAlquilerViewImpl.class.getName()).log(Level.SEVERE, null, ex);
        }


        List<Serializable> l=new ArrayList<Serializable>();

        l.add(String.valueOf(this.a.getIdAlquiler()));
        l.add(this.a.getCliente().getDni());
        l.add(String.valueOf(this.pistaSeleccionada.getIdPista()));
        l.add(hora);

        this.getController().actualizaEntidadGesture(l);
        //this.getModel().actualizarEntidad(a);
        JOptionPane.showMessageDialog(this, "Alquiler modificado con exito");

        }catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, "Los nuevos datos no son correctos");
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jHoraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jHoraActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jHoraActionPerformed

    private void listaPistasMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listaPistasMActionPerformed
        if (listaPistasM.getSelectedIndex() != -1) { // si se ha seleccionado alguna fecha

            pistaSeleccionada = pistaComboModel.getPistas().get(listaPistasM.getSelectedIndex());
            try{
            establecerHorasDisponibles();
            }catch(Exception ex)
            {
                JOptionPane.showMessageDialog(this, "Los nuevos datos no son correctos");
            }
        }
}//GEN-LAST:event_listaPistasMActionPerformed

    public void gestionarAccion(AlquilerImpl a) {

        this.setAlquiler(a);
        

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jApellidos;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JTextField jDNI;
    private javax.swing.JTextField jFecha;
    private javax.swing.JTextField jHora;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jListaAlquileres;
    private javax.swing.JTextField jNombre;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jPista;
    private javax.swing.JComboBox listaHoras;
    private javax.swing.JComboBox listaPistasM;
    // End of variables declaration//GEN-END:variables


    public void establecerHorasDisponibles(){


        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy HH:mm");
        String strFecha = jFecha.getText()+" 00:00";
        Date fecha = null;
        try {

            fecha = sdf.parse(strFecha);

        } catch (ParseException ex) {

            ex.printStackTrace();

        }
        DefaultComboBoxModel model = new DefaultComboBoxModel();

        for(String s:pistaSeleccionada.horasLibres(fecha))
        {
            model.addElement(s);
        }

        listaHoras.setModel(model);
        listaHoras.updateUI();
        listaHoras.setEnabled(true);



    }





    @Override
    protected void refresh() {
        List<AlquilerImpl> alquileres = this.getModel().listar();
        actualizarAlquileres(alquileres);
        this.pistaComboModel.setPistas(getPistaModel().listar());
        this.listaHoras.setModel(new DefaultComboBoxModel());
    }
}
